home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dicehelp / AutoRefs.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  5KB  |  221 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  AUTOREFS refsfile docfile docfile docfile
  9.  *
  10.  *  Given one or more autodoc or .H files (e.g. intuition.doc) or include
  11.  *  files (e.g. exec/types.h), this program appends to a dme.refs file
  12.  *  <refsfile> appropriate lines.
  13.  *
  14.  *  AUTOREFS determines the file type from the extension (.h for header
  15.  *  files, otherwise assumed to be a doc file).
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. char *SetSName(char *, char *);
  22.  
  23. main(xac, xav)
  24. int xac;
  25. char *xav[];
  26. {
  27.     short i;
  28.     FILE *fi;
  29.     FILE *fo;
  30.     int ac;
  31.     char **av;
  32.  
  33.     expand_args(xac, xav, &ac, &av);
  34.  
  35.     if (ac == 1) {
  36.     puts("autorefs outfile docfile docfile ...");
  37.     puts("AmigaDOS wildcarding works too, btw");
  38.     exit(1);
  39.     }
  40.     fo = fopen(av[1], "a");
  41.     if (!fo) {
  42.     printf("unable to open %s for append\n", av[1]);
  43.     exit(1);
  44.     }
  45.     for (i = 2; i < ac; ++i) {
  46.     char *file = av[i];
  47.     short len = strlen(file);
  48.     short doth = 0;
  49.  
  50.     if (len >= 2 && (file[len-1] == 'h' || file[len-1] == 'H') && file[len-2] == '.')
  51.         doth = 1;
  52.  
  53.     fi = fopen(file, "r");
  54.     if (fi) {
  55.         if (doth) {
  56.         printf("Scanning .H  file: %s\n", file);
  57.         scanhfile(fi, fo, file);
  58.         } else {
  59.         printf("Scanning DOC file: %s\n", file);
  60.         scandocfile(fi, fo, file);
  61.         }
  62.         fclose(fi);
  63.     } else {
  64.         printf("Unable to read %s\n", file);
  65.     }
  66.     }
  67.     return(0);
  68. }
  69.  
  70. /*
  71.  *  Find the headers for each function entry and generate a DME.REFS
  72.  *  entry for it.  The @@<N> is a short form seek position (this field
  73.  *  normally holds a search string).
  74.  */
  75.  
  76. scandocfile(fi, fo, filename)
  77. FILE *fi;
  78. FILE *fo;
  79. char *filename;
  80. {
  81.     char buf[256];
  82.     long pos = 0;
  83.     short lastLineFF = 0;
  84.  
  85.     while (fgets(buf, 256, fi)) {
  86.     short len = strlen(buf) - 1;
  87.     char *ptr = buf + len;
  88.     char *bas = buf;
  89.     char *header, *tail;
  90.  
  91.     buf[len] = 0;
  92.     while (ptr != buf && ptr[-1] != ' ' && ptr[-1] != 9)
  93.         --ptr;
  94.     while (bas < ptr && *bas == 12)
  95.         ++bas;
  96.     if (ptr != bas && *ptr && strncmp(bas, ptr, strlen(ptr)) == 0) {
  97.         if (buf[0] == 12) {
  98.         ++pos;
  99.         buf[0] = 0;
  100.         }
  101.         header = ptr;
  102.         for (ptr = buf + len; ptr != buf && IsAlphaNum(ptr[-1]); --ptr);
  103.         tail = ptr;
  104.         fprintf(fo, "%-20s (^l) %s @@%ld\n", tail, filename, pos);
  105.     } else if (ptr == bas && *ptr && lastLineFF) {
  106.         if (buf[0] == 12) {
  107.         ++pos;
  108.         buf[0] = 0;
  109.         }
  110.         for (ptr = buf + len; ptr != buf && IsAlphaNum(ptr[-1]); --ptr);
  111.         fprintf(fo, "%-20s (^l) %s @@%ld\n", ptr, filename, pos);
  112.     }
  113.     if (buf[0] == ('l'&0x1F))
  114.         lastLineFF = 1;
  115.     else
  116.         lastLineFF = 0;
  117.     pos = ftell(fi);
  118.     }
  119. }
  120.  
  121. /*
  122.  *  Find each structure definition (stupid search, assume struct on left
  123.  *  hand side) then generate dme.refs entry from the end point of the
  124.  *  previous structure to the beginning of the next structure.    That is,
  125.  *  the reference refers to the structure and all fields before and after
  126.  *  it until the next structure (before and after).
  127.  */
  128.  
  129. scanhfile(fi, fo, filename)
  130. FILE *fi;
  131. FILE *fo;
  132. char *filename;
  133. {
  134.     static char buf[256];
  135.     static char sname[128];
  136.     static char lname[128];
  137.     long lin  = 1;
  138.     long lin1;
  139.     long lin2 = 1;
  140.     long pos  = 0;
  141.     long pos1;
  142.     long pos2 = 0;
  143.     short snameisvalid = 0;
  144.     short newsname = 0;
  145.  
  146.     while (fgets(buf, 256, fi)) {
  147.     char *ptr = buf;
  148.  
  149.     if ((ptr = strstr(buf, "struct")) || (ptr = strstr(buf, "union"))) {
  150.         if (ptr[0] == 's')
  151.         ++ptr;
  152.         ptr += 5;
  153.  
  154.         ptr = SetSName(lname, ptr);
  155.  
  156.         /*
  157.          *    search for '{'
  158.          */
  159.  
  160.         {
  161.         while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n' || *ptr == 12)
  162.             ++ptr;
  163.         if (*ptr == 0) {
  164.             short c = ' ';
  165.             long savpos = ftell(fi);
  166.             while (c == ' ' || c == '\t' || c == '\n' || c == 12)
  167.             c = getc(fi);
  168.             ptr[0] = c;
  169.             ptr[1] = 0;
  170.             fseek(fi, savpos, 0);
  171.         }
  172.         }
  173.  
  174.         if (*ptr == '{' && lname[0]) {
  175.         if (snameisvalid)
  176.             fprintf(fo, "%-20s %3ld %s @@%ld\n", sname, lin-lin1, filename, pos1);
  177.         strcpy(sname, lname);
  178.         snameisvalid = 0;
  179.         newsname = 1;
  180.         pos1 = pos2;
  181.         lin1 = lin2;
  182.         }
  183.     }
  184.     pos = ftell(fi);
  185.     ++lin;
  186.  
  187.     if (strstr(buf, "}")) {
  188.         pos2 = pos;
  189.         lin2 = lin;
  190.         snameisvalid = newsname;
  191.     }
  192.     }
  193.     if (snameisvalid)
  194.     fprintf(fo, "%-20s %3ld %s @@%ld\n", sname, lin-lin1, filename, pos1);
  195. }
  196.  
  197. char *
  198. SetSName(buf, ptr)
  199. char *buf, *ptr;
  200. {
  201.     while (*ptr == ' ' || *ptr == 9)
  202.     ++ptr;
  203.     while (*ptr && *ptr != '\n' && *ptr != ' ' && *ptr != 9 && *ptr != 12)
  204.     *buf++ = *ptr++;
  205.     *buf = 0;
  206.     return(ptr);
  207. }
  208.  
  209. IsAlphaNum(c)
  210. char c;
  211. {
  212.     if ((c >= 'a' && c <= 'z') ||
  213.     (c >= 'A' && c <= 'Z') ||
  214.     (c >= '0' && c <= '9') ||
  215.     (c == '_') || (c == '(') || (c == ')')
  216.     )
  217.     return(1);
  218.     return(0);
  219. }
  220.  
  221.